package com.example.wastes.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wastes.common.pojo.User;
import com.example.wastes.user.mapper.UserMapper;
import com.example.wastes.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findall() {
        return userMapper.findall();
    }


    @Override
    public List<User> findUserName(String username) {
        return userMapper.findUserName(username);
    }

    @Override
    public User SelectIntegral(Integer id) {
        return userMapper.SelectIntegral(id);
    }

    @Override
    public int upload1(User user, MultipartFile fileUpload, HttpServletRequest request) {
        String originalFilename = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新生成文件名字
        originalFilename = UUID.randomUUID()+suffixName;
        //制定本地文件夹存储图片，写道需要保存的目录下
        String filePath = "D:\\ideaworkspeace\\images\\javaText\\";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+originalFilename));
            user.setUserimg("http://localhost:8080/wastes/" + originalFilename);
            return userMapper.updateById(user);
            //返回提示信息
        }catch(IOException e) {
            e.printStackTrace();
            throw new RuntimeException("头像上传失败");
        }
    }

    @Override
    public User tologin1(String username) {
        return userMapper.tologin1(username);
    }

    @Override
    public int saveUser(String username, String password) {
        return userMapper.saveUser(username, password);
    }

    @Override
    public User findName(String username) {
        return userMapper.findName(username);
    }

    @Override
    public List<User> findByidAll(Integer id) {
        return userMapper.findByidAll(id);
    }

    @Override
    public int updateMoney(Integer id) {
        return userMapper.updateMoney(id);
    }
}
